package com.huidian.gitbatchprocessing.ui.service.impl;

import com.huidian.gitbatchprocessing.git.util.GitUtil;
import com.huidian.gitbatchprocessing.ui.constant.DialogConstant;
import com.huidian.gitbatchprocessing.ui.enums.BranchOperateEnum;
import com.huidian.gitbatchprocessing.ui.service.AbstractBranchOperate;
import com.huidian.gitbatchprocessing.ui.service.dto.BaseBranchDto;
import com.huidian.gitbatchprocessing.ui.service.dto.CheckoutBranchDto;
import com.intellij.openapi.util.text.StringUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
import org.jetbrains.annotations.NotNull;

/**
 * 切换分支操作
 *
 * @author zxb
 * @date 2021/6/18
 */
public class CheckoutBranchOperateImpl extends AbstractBranchOperate {

    @Override
    public Byte getKey() {
        return BranchOperateEnum.CHECKOUT_BRANCH.getKey();
    }

    @Override
    public String execute(BaseBranchDto baseBranchDto, @NotNull Repository repository) throws Exception {
        CheckoutBranchDto checkoutBranchDto = (CheckoutBranchDto) baseBranchDto;
        String checkoutBranchName = checkoutBranchDto.getSourceBranchName();
        if (StringUtil.isEmpty(checkoutBranchName)) {
            throw new RuntimeException("分支名不能为空！");
        }
        Git git = new Git(repository);
        GitUtil.checkout(git, checkoutBranchName);
        return DialogConstant.SUCCESS;
    }
}
